package com.example.practice;

import com.example.practice.entity.Student;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.partitioningBy;

public class StreamTest {
    public static void main(String[] args) {
        // 测试数据
        List<Student> list = new ArrayList<>();
        list.add(new Student("李明", "男", 18));
        list.add(new Student("李飞", "男", 17));
        list.add(new Student("张丽", "女", 17));
        list.add(new Student("张艳", "女", 20));
        list.add(new Student("王朋", "男", 18));
        list.add(new Student("陈晴", "女", 18));
        list.add(new Student("王菲", "女", 19));
        list.add(new Student("李雷", "男", 20));

        // 查找性别为男的学生
        List<Student> boys = list.stream().filter(s -> "男".equals(s.getGender())).collect(Collectors.toList());
        System.out.println("1、查找性别为男的学生：");
        System.out.println(boys.toString());

        // 查找性别为女的学生
        List<Student> girls = list.stream().filter(s -> "女".equals(s.getGender())).collect(Collectors.toList());
        System.out.println("2、查找性别为女的学生：");
        System.out.println(girls.toString());

        // map的key值true为男，false为女的集合
        Map<Boolean, List<Student>> map1 = list.stream().collect(partitioningBy(student -> "男".equals(student.getGender())));
        System.out.println("3、map的key值true为男，false为女的集合：");
        System.out.println(map1);

        // 求性别为男的学生总岁数
        int sum = list.stream().filter(s -> "男".equals(s.getGender())).mapToInt(Student::getAge).sum();
        System.out.println("4、求性别为男的学生总岁数：");
        System.out.println(sum);

        // 按性别进行分组统计人数
        Map<String, Integer> map2 = list.stream().collect(Collectors.groupingBy(Student::getGender, Collectors.summingInt(p -> 1)));
        System.out.println("5、按性别进行分组统计人数：");
        System.out.println(map2);

        // 按性别进行分组统计各组
        Map<String, List<Student>> map3 = list.stream().collect(Collectors.groupingBy(Student::getGender));
        System.out.println("6、按性别进行分组统计各组：");
        System.out.println(map3);

        // 判断是否有年龄大于25岁的学生
        boolean check = list.stream().anyMatch(student -> student.getAge() > 25);
        System.out.println("7、判断是否有年龄大于25岁的学生：");
        System.out.println(check);

        // 获取所有学生的姓名集合
        List<String> list2 = list.stream().map(Student::getName).collect(Collectors.toList());
        System.out.println("8、获取所有学生的姓名集合：");
        System.out.println(list2);

        // 求所有人的平均年龄
        System.out.println("9、求所有人的平均年龄：");

        // 求年龄最大的学生
        Student s = list.stream().reduce((student, student2) -> student.getAge() > student2.getAge() ? student : student2).get();
        System.out.println("10、求年龄最大的学生：");
        System.out.println(s);

        // 按照年龄从小到大排序
        List<Student> list3 = list.stream().sorted((s1, s2) -> s1.getAge().compareTo(s2.getAge())).collect(Collectors.toList());
        System.out.println("11、按照年龄从小到大排序：");
        System.out.println(list3);

        // 获取所有的名字，输出一个字符串
        String str = list.stream().map(Student::getName).collect(Collectors.joining(",", "[", "]"));
        System.out.println("12、获取所有的名字，输出一个字符串：");
        System.out.println(str);

        // 获取年龄的最大值、最小值、平均值、求和
        IntSummaryStatistics intSummaryStatistics = list.stream().mapToInt(Student::getAge).summaryStatistics();
        System.out.println("13、获取年龄的最大值、最小值、平均值、求和：");
        System.out.println(intSummaryStatistics);
    }
}
